package b1.mobile.blackberry.ui.component;

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.container.VerticalFieldManager;


public class ListGroupItemManager extends VerticalFieldManager 
{
    public static final int MARGIN = 0;
    
    public ListGroupItemManager()
    {
        super( NO_VERTICAL_SCROLL );
        setMargin( MARGIN, MARGIN, MARGIN, MARGIN );
    }
    
    protected void sublayout( int maxWidth, int maxHeight )
    {
        super.sublayout( maxWidth, maxHeight );
        
        int numChildren = this.getFieldCount();
        if( numChildren > 0 ) {
            if( numChildren == 1 ) {
                Field child = getField( 0 );
                if( child instanceof ListGroupItemField ) {
                    ( (ListGroupItemField) child ).setDrawPosition( ListGroupItemField.DRAWPOSITION_SINGLE );
                }
            } else {
                int index = 0;
                Field child = getField( index );
                if( child instanceof ListGroupItemField ) {
                    ( (ListGroupItemField) child ).setDrawPosition( ListGroupItemField.DRAWPOSITION_TOP );
                }
                for( index = 1; index < numChildren - 1 ; index++ ) {
                    child = getField( index );
                    if( child instanceof ListGroupItemField ) {
                        ( (ListGroupItemField) child ).setDrawPosition( ListGroupItemField.DRAWPOSITION_MIDDLE );
                    }
                }
                child = getField( index );
                if( child instanceof ListGroupItemField ) {
                    ( (ListGroupItemField) child ).setDrawPosition( ListGroupItemField.DRAWPOSITION_BOTTOM );
                }
            }
        }
    }
}
